package priv.mill.net;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.List;

import org.apache.log4j.Logger;

public class MyDecoder extends ByteToMessageDecoder {
	private static Logger logger=Logger.getLogger(MyDecoder.class);
	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in,
			List<Object> out) throws Exception {
		byte[] dst = new byte[in.readableBytes()];
		in.readBytes(dst);
		MyHttp info = fromBytes(dst);
		logger.debug(info.getType());
		// String info = new String(dst);
		out.add(info);
	}

	private MyHttp fromBytes(byte[] dst) throws Exception {
		try(ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				dst);
		ObjectInputStream inputStream = new ObjectInputStream(
				byteArrayInputStream);){
			
			return (MyHttp) inputStream.readObject();
		}
		
	}
}
